package com.nf.health.app.intervention_details.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.intervention_details.bean.FoodDetailsBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 恩福 on 2016/11/14.
 * 推荐食谱 Adapter
 *
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class RecommendRecipeAdapter<T> extends RecyclerView.Adapter {

    private List<T> data;
    private Context mContext;
    public static final int ITEM_HEAD = 0;

    public RecommendRecipeAdapter(Context context, List<T> data) {
        this.mContext = context;
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.health_info_scheme_result_item, parent, false);
        return new RecommendRecipeHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        RecommendRecipeHolder recommendRecipeHolder = (RecommendRecipeHolder) holder;
        if (position % 2 == 0) {
            recommendRecipeHolder.llContainer.setBackground(null);
        } else {
            recommendRecipeHolder.llContainer.setBackgroundColor(mContext.getResources().getColor(R.color.gray3_1));
        }
        if (ITEM_HEAD == position) {
            recommendRecipeHolder.tvFoodType.setTextColor(mContext.getResources().getColor(R.color.orange));
            recommendRecipeHolder.tvCaloriePercent.setTextColor(mContext.getResources().getColor(R.color.orange));
            recommendRecipeHolder.tvFoodExample.setTextColor(mContext.getResources().getColor(R.color.orange));
            recommendRecipeHolder.tvFoodType.setText("食物类别");
            recommendRecipeHolder.tvCaloriePercent.setText("卡路里配比");
            recommendRecipeHolder.tvFoodExample.setText("食物举例");
        } else {
            T dataBean = data.get(position - 1);
            if (dataBean instanceof FoodDetailsBean.MornBean) {
                FoodDetailsBean.MornBean mornBean = (FoodDetailsBean.MornBean) dataBean;
                recommendRecipeHolder.tvFoodType.setText("" + mornBean.getType());
                recommendRecipeHolder.tvCaloriePercent.setText("" + mornBean.getCalorie());
                recommendRecipeHolder.tvFoodExample.setText("" + mornBean.getName());
            }
            if (dataBean instanceof FoodDetailsBean.NoonBean) {
                FoodDetailsBean.NoonBean mornBean = (FoodDetailsBean.NoonBean) dataBean;
                recommendRecipeHolder.tvFoodType.setText("" + mornBean.getType());
                recommendRecipeHolder.tvCaloriePercent.setText("" + mornBean.getCalorie());
                recommendRecipeHolder.tvFoodExample.setText("" + mornBean.getName());
            }
            if (dataBean instanceof FoodDetailsBean.NightBean) {
                FoodDetailsBean.NightBean mornBean = (FoodDetailsBean.NightBean) dataBean;
                recommendRecipeHolder.tvFoodType.setText("" + mornBean.getType());
                recommendRecipeHolder.tvCaloriePercent.setText("" + mornBean.getCalorie());
                recommendRecipeHolder.tvFoodExample.setText("" + mornBean.getName());
            }
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 1 : data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }


    class RecommendRecipeHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_target)
        TextView tvFoodType;
        @Bind(R.id.tv_new_result)
        TextView tvCaloriePercent;
        @Bind(R.id.tv_reference)
        TextView tvFoodExample;
        @Bind(R.id.ll_container)
        LinearLayout llContainer;

        public RecommendRecipeHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
